package com.schooldate.model.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.schooldate.base.BaseAction;
import com.schooldate.model.entity.MessageBoard;
import com.schooldate.model.entity.School;
import com.schooldate.model.entity.UserInfo;
import com.schooldate.util.CommomUtils;
import com.schooldate.util.SimpleJsonFilter;
import org.apache.struts2.ServletActionContext;
import org.springframework.aop.IntroductionAdvisor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Enumeration;

/**
 * Created by etund on 2015/3/21 0021.
 */
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class MessageBoardAction extends BaseAction<MessageBoard> {

    private String userId;
    private String sid;
    private static  Integer maxNum = 20;


    /**
     * 发送留言
     * @throws Exception
     */
    public void sendMessageBoard() throws Exception{
        UserInfo userInfo  = this.userInfoService.getById(Long.parseLong(userId.trim()));
//        设置文件路径
        if (file!=null&&file.length>0){
            uploadFileReturnPath("messageBoardImage");
            if (model.getImagePath() != null) {
                model.setImagePath(model.getImagePath() + ","+fileFullPath);
            } else {
                model.setImagePath(fileFullPath);
            }
        }

        model.setSendTime(CommomUtils.getCurrentTime());
        model.setUserInfo(userInfo);
        model.setMessageBoardId(model.getId());
        this.messageBoardService.save(model);
        model.setMessageBoardId(model.getId());
        this.messageBoardService.update(model);
        object.put("code",1);
        result = JSON.toJSONString(object);
        renderJson(result);
    }


    /**
     * 获取留言板内容，第一次显示20条
     * 刷新留言板
     * @throws Exception
     */
    public void getMessageBoard() throws Exception{
        if (refreshNum!=null&&!"".equals(refreshNum.trim()))
            maxNum += Integer.parseInt(refreshNum);
        UserInfo userInfo = this.userInfoService.getById(Long.parseLong(userId));
        School school = userInfo.getSchool();
        messageBoards = this.messageBoardService.getBySchoolId(Long.parseLong(school.getSid()),maxNum);
        SimpleJsonFilter filter = new SimpleJsonFilter("messageBoards.do");
        object.put("code",1);
        object.put("messageBoards",messageBoards);
        result = JSON.toJSONString(object,filter, SerializerFeature.DisableCircularReferenceDetect);
        renderJson(result);
        }



        public String getUserId() {
                return userId;
                }

        public void setUserId(String userId) {
                this.userId = userId;
                }

        public String getSid() {
                return sid;
                }

        public void setSid(String sid) {
                this.sid = sid;
                }

        }
